package com.doctcloud.hisystem.common.controller;

import com.doctcloud.common.core.utils.StringUtils;
import com.doctcloud.common.core.utils.poi.ExcelUtil;
import com.doctcloud.common.core.web.controller.BaseController;
import com.doctcloud.common.core.web.domain.AjaxResult;
import com.doctcloud.common.core.web.page.TableDataInfo;
import com.doctcloud.common.log.annotation.Log;
import com.doctcloud.common.log.enums.BusinessType;
import com.doctcloud.common.security.annotation.RequiresPermissions;
import com.doctcloud.common.security.utils.SecurityUtils;
import com.doctcloud.hisystem.common.service.ISysDictDataEntityService;
import com.doctcloud.hisystem.common.service.ISysDictTypeEntityService;
import com.doctcloud.hisystem.tables.domain.common.SysDictDataEntity;
import com.doctcloud.hisystem.tables.domain.common.SysDictTypeEntity;
import com.doctcloud.system.api.domain.SysDictData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author :  doctCloud samula
 * @date :  2025/11/25
 * @Descript : 数据字典信息
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataEntityController extends BaseController {
    @Autowired
    private ISysDictDataEntityService dictDataService;
    @Autowired
    private ISysDictTypeEntityService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @GetMapping("/list")
    public TableDataInfo list(SysDictDataEntity dictData)
    {
        startPage();
        List<SysDictDataEntity> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    @Log(title = "系统字典数据", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictDataEntity dictData)
    {
        List<SysDictDataEntity> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictDataEntity> util = new ExcelUtil<SysDictDataEntity>(SysDictDataEntity.class);
        util.exportExcel(response, list, "系统字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictTypeEntity> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictTypeEntity>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDictDataEntity dict)
    {
        dict.setCreateBy(SecurityUtils.getUsername());
        dict.setCreateTime(new Date());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysDictDataEntity dict)
    {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable List<Long> dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
